package com.cac.demo.controller.fileHandling;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;

@RestController
@RequestMapping("/cemopHandoverForm")
@Slf4j
public class FileSender {

    @PostMapping("/sendFileToCemop")
    public static void sendFileToCemop(String zipFilePath, String proxyUrl, String cemopApiUrl) {
        RestTemplate restTemplate = new RestTemplate();

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 准备文件资源
        FileSystemResource fileResource = new FileSystemResource(new File(zipFilePath));

        // 创建请求体
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", fileResource);
        body.add("cemopApiUrl", cemopApiUrl);

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        // 发送请求
        ResponseEntity<String> response = restTemplate.postForEntity(proxyUrl, requestEntity, String.class);

        // 处理响应
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println("File sent successfully!");
        } else {
            System.out.println("Failed to send file. Response code: " + response.getStatusCode());
        }
    }

    /*public static void main(String[] args) {
        String zipFilePath = "/path/to/your/local/folder/output.zip";
        String proxyUrl = "http://proxy-service-url/upload";
        String targetUrl = "http://target-service-url/endpoint";

        sendFile(zipFilePath, proxyUrl, targetUrl);
    }*/
}
